/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-10-24         luwei
 */

package com.tianci.appstore.ui.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.NinePatch;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.widget.SkyCategoryName.Category;
import com.tianci.appstore.utils.SkyConstants;

public class SkyCategoryCursor extends View {

    private NinePatch mFocusCursor = null;
    private NinePatch mSelectCursor = null;
    private Context mContext = null;
    private List<Cursor> mCursors = null;
    private int mCurrentPos = 0;
    private int mTempX = 0;
    private int mTempY = 0;
    private int mDefaultStep = SkyConstants.Div(60);
    private int mStep = mDefaultStep;
    private Bitmap mFocusBitmap = null;
    private Bitmap mSelectBitmap = null;


    public SkyCategoryCursor(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
        // TODO Auto-generated constructor stub
    }

    public SkyCategoryCursor(Context context) {
        super(context);
        this.mContext = context;
        setWillNotDraw(false);
        init();

    }

    private void init() {
        invalidate();
    }

    public void setData(List<Category> categories) {
        mCursors = new ArrayList<Cursor>();
        mFocusBitmap = SkinManager.getResourceBitmap(R.drawable.top_btn_focus);
        if (mFocusBitmap == null) {
            mFocusBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.top_btn_focus);
        }
        mFocusCursor = new NinePatch(mFocusBitmap, mFocusBitmap.getNinePatchChunk(), null);
        mSelectBitmap = SkinManager.getResourceBitmap(R.drawable.top_btn);
        if (mSelectBitmap == null) {
            mSelectBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.top_btn);
        }
        mSelectCursor = new NinePatch(mSelectBitmap, mSelectBitmap.getNinePatchChunk(), null);
        int i = 0;
        for (Category category : categories) {
            Cursor cursor = new Cursor();
            if (i == 0) {
                cursor.mDesX = 0;
            } else {
                cursor.mDesX = mCursors.get(i - 1).mDesX + SkyConstants.Div(90) + categories.get(i - 1).mWidth;
            }
            cursor.mLength = SkyConstants.Div(120) + category.mWidth;
            LogUtils.d("cursor width is " + cursor.mLength + " and cursor x is " + cursor.mDesX);
            cursor.mDesY = 0;
            i++;
            mCursors.add(cursor);
        }
    }

    public void onDestory() {
        if (mFocusBitmap != null && !mFocusBitmap.isRecycled()) {
            mFocusBitmap.recycle();
            mFocusBitmap = null;
        }
        if (mSelectBitmap != null && !mSelectBitmap.isRecycled()) {
            mSelectBitmap.recycle();
            mSelectBitmap = null;
        }
    }

    private boolean mIsFirst = true;

    public void setFocusPos(int pos) {
        LogUtils.d("the currentpos is " + mCurrentPos);
        LogUtils.d("the pos is " + pos);
        if (mIsFirst) {
            mStep = 200;
            mIsFirst = false;
        } else {
            if (mCurrentPos == pos) {
                mStep = mDefaultStep;
            } else {
                mStep = mDefaultStep * Math.abs(mCurrentPos - pos);
            }
        }

        this.mCurrentPos = pos;
        if (mCursors != null && mCursors.size() > pos)
            mCursors.get(pos).beginMove();
    }

    public void setFocusPos(int pos, boolean hasAnim) {
        if (mCursors.size() == 0) {
            return;
        }
        if (hasAnim)
            setFocusPos(pos);
        else {
            LogUtils.d("the currentpos is " + mCurrentPos);
            LogUtils.d("the pos is " + pos);
            mTempX = mCursors.get(pos).mDesX;
            this.mCurrentPos = pos;
            invalidate();
        }
    }

    public boolean setNextLeftPos() {
        if (mCurrentPos > 0) {
            mCurrentPos--;
            mStep = mDefaultStep;
            mCursors.get(mCurrentPos).beginMove();
            return true;
        }
        return false;
    }

    public int getFocusPos() {
        return mCurrentPos;
    }

    public boolean setNextRightPos() {
        if (mCurrentPos < mCursors.size() - 1) {
            mCurrentPos++;
            mStep = mDefaultStep;
            mCursors.get(mCurrentPos).beginMove();
            return true;
        }
        return false;
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mCursors.size() > 0) {
            Rect rect = new Rect(mTempX, mTempY, mTempX + mCursors.get(mCurrentPos).mLength, mTempY + SkyConstants.Div(110));
            if (isSelected()) {
                mSelectCursor.draw(canvas, rect);
            } else {
                mFocusCursor.draw(canvas, rect);
            }
            if (mCursors != null) {
                mCursors.get(mCurrentPos).beginMove();
            }
        }
    }

    public class Cursor {
        public int mDesX;
        public int mDesY;
        public int mLength;

        public void beginMove() {
            if (mDesX != mTempX) {
                if (mDesX - mTempX > mStep) {
                    mTempX += mStep;

                } else if (mDesX - mTempX < -mStep) {
                    mTempX -= mStep;
                } else {
                    mTempX = mDesX;

                }
                invalidate();
            }
        }
    }
}
